home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / chrome / content / folder_chooser.js < prev    next >
Text File  |  2009-07-17  |  4KB  |  111 lines

  1. var FolderChooser = function()
  2. {
  3.     var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  4.  
  5.     function LOG(msg)
  6.     {
  7.         var enabled = false;
  8.         if(enabled)
  9.         {
  10.             var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  11.             consoleService.logStringMessage("smarterwiki smarterwiki.js: " + msg);
  12.         }
  13.     }
  14.  
  15.     var path_to_nsILocalFile = function(path)
  16.     {
  17.         var dir = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  18.         dir.initWithPath(path);
  19.         return dir;
  20.     };
  21.     
  22.     $(document).ready(function()
  23.     {
  24.         var dest_folder_textbox = document.getElementById("dest-folder-textbox");
  25.         dest_folder_textbox.value = prefManager.getCharPref("browser.download.lastDir");
  26.         var set_accept_button_status = function()
  27.         {
  28.             var path = dest_folder_textbox.value;
  29.             var disable_accept = true;
  30.             try
  31.             {
  32.                 var dir = path_to_nsILocalFile(path);
  33.                 if(!dir.exists() || dir.isDirectory())
  34.                 {
  35.                     disable_accept = false;
  36.                 }
  37.             } 
  38.             catch(e) 
  39.             {
  40.                 LOG(e);
  41.             }
  42.             document.documentElement.getButton("accept").setAttribute("disabled", disable_accept);
  43.         };            
  44.         dest_folder_textbox.addEventListener("keydown", function(event)
  45.         {
  46.             set_accept_button_status();
  47.         }, true);
  48.         set_accept_button_status();
  49.     });
  50.     
  51.     return {
  52.         ondialogaccept: function()
  53.         {
  54.             try
  55.             {
  56.                 var dest_folder_textbox = document.getElementById("dest-folder-textbox");
  57.                 var dir = path_to_nsILocalFile(dest_folder_textbox.value);
  58.                 if(!dir.exists()) 
  59.                 {
  60.                     var permissions = dir.parent.exists() ? dir.parent.permissions : 0600;
  61.                     dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, permissions);
  62.                 }
  63.                 prefManager.setCharPref("browser.download.lastDir", dir.path);
  64.                 //CANNOT use setTimeout because window might be destroyed first,
  65.                 window.arguments[0](dir);
  66.             }
  67.             catch(e)
  68.             {
  69.                 LOG(e);
  70.             }
  71.         },
  72.  
  73.         ondialogcancel: function()
  74.         {
  75.         },
  76.  
  77.         onbrowsecommand: function()
  78.         {
  79.             var strbundle = document.getElementById("smarterwiki_strings");
  80.             var select_folder_str = strbundle.getString("select_folder_str");
  81.  
  82.             var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  83.             filePicker.init(window, select_folder_str, Components.interfaces.nsIFilePicker.modeGetFolder);
  84.  
  85.             var dest_folder_textbox = document.getElementById("dest-folder-textbox");
  86.             try 
  87.             {
  88.                 var dir = path_to_nsILocalFile(dest_folder_textbox.value.toString());
  89.                 if(!dir.exists())
  90.                 {
  91.                     dir = dir.parent;
  92.                 }
  93.                 if(dir.exists() && dir.isDirectory()) 
  94.                 {
  95.                     filePicker.displayDirectory = dir;
  96.                 }
  97.             } 
  98.             catch(e)
  99.             {
  100.             }
  101.  
  102.             filePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
  103.  
  104.             if(filePicker.show() == Components.interfaces.nsIFilePicker.returnOK) 
  105.             {
  106.                 dest_folder_textbox.value = filePicker.file.path;
  107.                 document.documentElement.getButton("accept").setAttribute("disabled", false);
  108.             }
  109.         },
  110.     };
  111. }();